import { ArticleListRespones } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import { http } from '@/utils/http'

/**
 *  根据id获取对应文章列表
 * @param id 获取id
 * @returns 文章列表
 */
export const getArticleListByChannelId = (
  channelId: number,
  timestamp: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleListRespones>(`/articles`, {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })
    // console.log('获取数据', res)
    dispatch({
      type: 'Home/getChannelsIdList',
      payload: {
        channelId,
        articleList: res.data.data,
      },
    })
  }
}

/**
 *
 * @returns
 */
export const getNewestArticleList = (
  channelId: number,
  timestamp: string | null
): RootThunkAction => {
  return async (dispatch) => {
    // console.log(channelId, timestamp)
    const res = await http.get<ArticleListRespones>('/articles', {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })

    console.log('下拉的数据流', res)

    dispatch({
      type: 'Home/getNewestArticleList',
      payload: {
        channelId,
        articleList: res.data.data,
      },
    })
  }
}
